package com.ygqh.baby.controller.admin;

import com.ygqh.baby.ao.ProductStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResponseEntity;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.model.EditProductPackRequest;
import com.ygqh.baby.model.ProductPackQueryRequest;
import com.ygqh.baby.model.YgProductPackExt;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.service.YgProductPackDetailService;
import com.ygqh.baby.service.YgProductPackV2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 自选包第二版
 * 把商品信息抽离出来
 * 增加自选包是否可用券
 * 自选包数量是否累计
 *
 * @author guohao
 * @date 2018-12-11
 */
@Controller
@RequestMapping("/admin/productpackv2")
public class ProductPackV2Controller extends BaseController<Object, Long> {

    @Autowired
    private YgProductPackV2Service ygProductPackV2Service;

    @Autowired
    private YgProductPackDetailService ygProductPackDetailService;

    /**
     * 获取自选包列表
     *
     * @param queryRequest 检索条件
     * @return Map<String                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Object>
     */
    @RequestMapping("search")
    @ResponseBody
    public Map<String, Object> search(ProductPackQueryRequest queryRequest) {

        ResultSet<YgProductPackExt> resultSet = ygProductPackV2Service.search(queryRequest);
        return resultSet.toDatatables(null);
    }

    /**
     * 编辑自选包
     *
     * @param packRequest 自选包信息
     * @return ResponseEntity
     */
    @RequestMapping("editProductPack")
    @ResponseBody
    public ResponseEntity editProductPack(@RequestBody EditProductPackRequest packRequest) {

        AdminUser session = getSession();
        packRequest.setHandler(session.getTrueName());
        return ygProductPackV2Service.editProductPack(packRequest);

    }

    @RequestMapping("deleteDetail")
    @ResponseBody
    public ResponseEntity deleteDetail(@RequestParam Long detailId) {

        AdminUser session = getSession();
        int i = ygProductPackDetailService.deleteByDetailId(detailId, session.getTrueName());
        return ResponseEntity.success(i);

    }

    /**
     * 获取自选包详情
     *
     * @param packId 自选包id
     * @return YgProductPackExt
     */
    @RequestMapping("getProductPackInfo")
    @ResponseBody
    public ResponseEntity getProductPackInfo(Long packId) {
        YgProductPackExt productPackInfo = ygProductPackV2Service.getProductPackInfo(packId);
        return ResponseEntity.success(productPackInfo);
    }

    @RequestMapping("findProductListWithPackSign")
    @ResponseBody
    public Map<String, Object> findProductListWithPackSign(QueryInfo queryInfo, Long productPackId, String productName, Long groupId) {
        if (queryInfo.getEnd() == 0) {
            queryInfo.setEnd(200);
        }
        List<YgProduct> list = ygProductPackV2Service.findProductListWithPackSign(queryInfo, productName, groupId, ProductStatus.OnSale, null,
                productPackId);
        ResultSet<YgProduct> result = new ResultSet<YgProduct>(list.size(), list);
        return result.toDatatables(null);

    }

}
